<?php
namespace Admin\Controller;
use Think\Controller;
class GradeController extends CommonController 
{
	//授课阶段年级表  分类
    public function index()
    {
    	//年级分类显示
    	$grades = M('grade')->select();
    	$grades = gettree($grades);
    	$this->assign('grades',$grades);

    	//分页显示
		// $grades = M('grade'); // 实例化User对象
		// $count      = $grades->count();// 查询满足要求的总记录数
		// $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		// $show       = $Page->show();// 分页显示输出
		// // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		// $list = $grades->limit($Page->firstRow.','.$Page->listRows)->select();
  //   	$this->assign('list',$list);
		// $this->assign('page',$show);// 赋值分页输出

        $this->display();
    }

    //年级分类 课程添加
    public function add()
    {
        if (IS_POST) {
            $data['grade_name'] = I('post.name');
            $data['parent_id'] = I('post.fid');

            if (empty($data['grade_name'])) {
                $arr = array(
                    "error" => 0,
                    "info" => "分类不能为空"
                    );
                echo json_encode($arr);
                exit;
            }

            $res = M('grade')->add($data);
            if ($res) {
                $arr = array(
                "error" => 1,
                "info" => "创建成功",
                'id' => $res
                );
            echo json_encode($arr);
            }else {
                $arr = array(
                    "error" => 0,
                    "info" => "创建失败"
                    );
                echo json_encode($arr);
            }
        }
    }

    //批量删除
    public function deletes()
    {
      //print_r($_POST);exit;
      //Array ( [arr] => Array ( [0] => 1 [1] => 2 ) )
      if (IS_POST) {
        $arr = I('post.arr');
        $ids = implode(",", $arr);

        //删除
        $res = M('grade')->delete($ids);
          if ($res) {
              $arr = array(
                  'error' =>1,
                  'info'=>"删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>0,
                  'info'=>"删除失败"
                  );
              echo json_encode($arr);
          }
      }
    }   

    //彻底删除
    public function delete()
    {
       // print_r($_POST);exit;
      if (IS_POST) {
          $gradeid = I('post.gradeid');

          $sql = 'delete from grade where gradeid='.$gradeid;
          $is_del = M()->execute($sql);
          if ($is_del) {
              $arr = array(
                  'error' =>1,
                  'info'=>"彻底删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>0,
                  'info'=>"彻底删除失败"
                  );
              echo json_encode($arr);
          }
      }
    }

    //编辑显示
    public function edit()
    {
    	$id = I('get.id');
    	$grades_info = M('grade')->where(array('gradeid'=>$id))->find();
    	$this->assign('grades_info',$grades_info);

    	//年级分类显示
    	$grades = M('grade')->select();
    	$grades = gettree($grades);
    	$this->assign('grades',$grades);


    	$this->display();
    }

    //edit
    public function edit_row()
    {
        //print_r($_POST);exit;
        if (IS_POST) {
            $id = I('post.gradeid');
            $data['grade_name'] = I('post.grade_name');
            $data['parent_id'] = I('post.fid');

            $res = M('grade')->where(array('gradeid'=>$id))->save($data);
            if ($res) {
                $arr = array(
                "error" => 1,
                "info" => "修改成功"
                );
            echo json_encode($arr);
            }else {
                $arr = array(
                    "error" => 0,
                    "info" => "修改失败"
                    );
                echo json_encode($arr);
            }
        }
    }




}